package com.ithiema.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ithiema.reggie.common.ApplicationContext;
import com.ithiema.reggie.common.R;
import com.ithiema.reggie.pojo.ShoppingCart;
import com.ithiema.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据：{}",shoppingCart);
        //设置用户id，指定当前数据库数据
        Long currentId = (Long)ApplicationContext.getData();
        shoppingCart.setUserId(currentId);
        Long dishId = (Long) shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
     queryWrapper.eq(ShoppingCart::getUserId,currentId);
     if (dishId!=null){
         queryWrapper.eq(ShoppingCart::getDishId,dishId);
     }else {
         queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
     }
     //查询当前菜品是否在购物车中
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        if(cartServiceOne != null){
            //如果已经存在，就在原来数量基础上加一
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
            cartServiceOne.setUserId(currentId);
        }else{
            //如果不存在，则添加到购物车，数量默认就是一

            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);
    }

    //查询购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,ApplicationContext.getData());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long userId = (Long)ApplicationContext.getData();
        log.info("取消购物车，当前userId:{}",userId);
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        Long dishId = shoppingCart.getDishId();
        if (dishId !=null){
            wrapper.eq(ShoppingCart::getDishId,dishId);

        }else{
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        Integer number = one.getNumber();
        number--;
        one.setNumber(number);
        if (number>0){
            shoppingCartService.updateById(one);
        }else{
            shoppingCartService.remove(wrapper);
        }
        return R.success(one);
    }

//    清空购物车
    @DeleteMapping("/clean")
public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,ApplicationContext.getData());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }
}

